package com.kyxx.topics.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kyxx.topics.BaseActivity;
import com.kyxx.topics.BaseActivity2;
import com.kyxx.topics.BaseApp;
import com.kyxx.topics.MainActivity;
import com.kyxx.topics.R;
import com.kyxx.topics.bean.ArticleBean;
import com.kyxx.topics.net.OkGoCallBack;
import com.kyxx.topics.net.OkGoRequest;
import com.kyxx.topics.net.ResponseListener;
import com.kyxx.topics.net.RestClient;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.youth.banner.Banner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import cz.msebera.android.httpclient.Header;
import okhttp3.Call;
import okhttp3.Response;

public class WelcomeActivity extends BaseActivity2 {
    private Banner banner;
    private ImageView iv;
    private TextView tv_time;
    private List<ArticleBean> articleBeanList = new ArrayList<>();
    private int delayTime = 2;
    DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
            .cacheOnDisk(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .showImageOnLoading(R.mipmap.recover)
            .showImageForEmptyUri(R.mipmap.recover)
            .showImageOnFail(R.mipmap.recover)
            .build();
    ;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (delayTime > 0) {
                        tv_time.setText(delayTime + "s跳过广告");
                        delayTime = delayTime - 1;
                        handler.sendEmptyMessageDelayed(0, 1000);
                    } else {
                        handler.removeMessages(0);
                        startActivity(new Intent(context, MainActivity.class));
                        finish();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(WelcomeActivity.this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(WelcomeActivity.this);
    }

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_welcome);
        BaseApp.getInstance().addActivity(this);
        setStatusBar();
    }

    @Override
    protected void findViewById() {
        banner = (Banner) findViewById(R.id.banner);
        iv = (ImageView) findViewById(R.id.iv);
        tv_time = (TextView) findViewById(R.id.tv_time);
    }

    @Override
    protected void setListener() {
        tv_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handler.removeMessages(0);
                startActivity(new Intent(context, MainActivity.class));
                finish();
            }
        });
    }

    @Override
    protected void processLogic() {
        initAdv();
    }

    private void initGuide() {

    }

    private void initAdv() {
        OkGoRequest.get(UrlUtils.ADLIST, this, "adlist", CacheMode.REQUEST_FAILED_READ_CACHE, new StringCallback() {
            @Override
            public void onCacheSuccess(String s, Call call) {
                super.onCacheSuccess(s, call);
                onSuccess(s, call, null);
            }

            @Override
            public void onCacheError(Call call, Exception e) {
                super.onCacheError(call, e);
                startActivity(new Intent(context, MainActivity.class));
            }

            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    JSONObject result = new JSONObject(s);
                    articleBeanList = JSON.parseArray(result.getJSONArray("data").toString(), ArticleBean.class);
                    imageLoader.loadImage(UrlUtils.BASEHTTP + articleBeanList.get(articleBeanList.size() - 1).getImgurl().getPath(), options, new ImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String imageUri, View view) {

                        }

                        @Override
                        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                            startActivity(new Intent(context, MainActivity.class));
                        }

                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                            iv.setImageBitmap(loadedImage);
                            tv_time.setVisibility(View.VISIBLE);
                            tv_time.setText(delayTime + "s跳过广告");
                            handler.sendEmptyMessageDelayed(0, 0);
                        }

                        @Override
                        public void onLoadingCancelled(String imageUri, View view) {

                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    startActivity(new Intent(context, MainActivity.class));
                }
            }
        });
    }

    private void setStatusBar() {
        int sdk = Build.VERSION.SDK_INT;
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        if (sdk >= Build.VERSION_CODES.LOLLIPOP) {
            int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            // 设置透明状态栏
            if ((params.flags & bits) == 0) {
                params.flags |= bits;
                window.setAttributes(params);
            }
        }
    }
}
